<?php

defined('YD_TRUE') or exit('Access Denied');

/*

// @author : lael

// blog : http://hi.baidu.com/lael80

// http://www.gzyd.net

// all right reserved.

*/

function formattime($time, $format = 'Y-m-d H:i:s', $default = '-'){

	if(empty($time))return $default;

	if(is_numeric($time))return date($format, $time);

	return strtotime($time);

}



function siteurl($params = array(), $host = true){

	$querystring = is_array($params) ? http_build_query($params) : $params;

	$url = '';

	if($host)$url = ((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']=='on')?'https://':'http://').$_SERVER['HTTP_HOST'];

	return $url.YD_URL.($querystring ? "?$querystring" : '');

}



function issubmit($post = true){

	$json = strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false || $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';

	if(!$post)return $json;

	return $json || $_SERVER['REQUEST_METHOD'] == 'POST';

}



function exitjson($code = 1, $msg = '操作执行成功', $data = array()){

	echo json_encode(array('code' => $code, 'msg' => $msg, 'data' => $data));

	exit;

}



function redirect($params = array(), $host = true){

	@header('location:'.siteurl($params, $host));

	exit;

}



function checknull($var, $default = ''){

	if(is_null($var))return $default;

	return $var;

}



function show_message($msg, $redirect = 'javascript:history.back(-1);', $timeout = 10000, $isexit = true){

	$html = file_get_contents(YD_PATH.'data/resource/show_message.html');

	echo str_ireplace(array('{$msg}', '{$redirect}', '{$timeout}'), array($msg, $redirect, $timeout), $html);

	if($isexit)exit;

}



function log_message($var, $key = ''){

	file_put_contents(YD_PATH.'data/logs/'.date('Ymd').'.txt', ($key ? $key.": " : '').date('Y-m-d H:i:s')." ---> \n".var_export($var, true)."\n\n\n", FILE_APPEND);

}



//href链接是用js替换的

function showpage($params = array(), $shownum = 10){

	$html = '共 '.$params['total'].' 条记录 ';

	if($params['pagenum'] <= 1)return $html;

	if($params['page'] > 1){

		$html .= '<a href="javascript:;" data-page="1">&lt;&lt;</a><a href="javascript:;" data-page="'.($params['page'] - 1).'">&lt;</a>';	

	}

	$first = max(1, $params['page'] - 4);

	$last = min($params['pagenum'], $first + $shownum - 1);

	if($last - $first < $shownum - 1){

		while($last < $params['pagenum']){

			if($last - $first == $shownum - 1)break;

			$last ++;

		}

	}

	if($last - $first < $shownum - 1){

		while($first > 1){

			if($last - $first == $shownum - 1)break;

			$first --;

		}

	}

	if($first > 1)$html .= '<a href="javascript:;" data-page="'.($first - 1).'">...</a>';

	for($i = $first; $i <= $last; $i ++){

		if($params['page'] == $i){

			$html .= '<strong>'.$i.'</strong>';

		}else{

			$html .= '<a href="javascript:;" data-page="'.$i.'">'.$i.'</a>';

		}

	}

	if($last < $params['pagenum'])$html .= '<a href="javascript:;" data-page="'.($last + 1).'">...</a>';

	if($params['page'] < $params['pagenum']){

		$html .= '<a href="javascript:;" data-page="'.($params['page'] + 1).'">&gt;</a><a href="javascript:;" data-page="'.($params['pagenum']).'">&gt;&gt;</a>';	

	}

	return $html;

}



function add_mqg(&$var) {

	if(is_array($var)){

		foreach($var as $k => $v){

			if(is_array($v)){

				add_mqg($var[$k]);

			}else{

				$var[$k] = addslashes($v);

			}

		}

	}else{

		$var = addslashes($var);

	}

}



function strip_mqg(&$var) {

	if(is_array($var)){

		foreach($var as $k => $v){

			if(is_array($v)){

				strip_mqg($var[$k]);

			}else{

				$var[$k] = stripslashes($v);

			}

		}

	}else{

		$var = stripslashes($var);

	}

}


function function_usable($function_name){
	static $_suhosin_func_blacklist;

	if (function_exists($function_name))
	{
		if ( ! isset($_suhosin_func_blacklist))
		{
			$_suhosin_func_blacklist = extension_loaded('suhosin')
				? explode(',', trim(ini_get('suhosin.executor.func.blacklist')))
				: array();
		}

		return ! in_array($function_name, $_suhosin_func_blacklist, TRUE);
	}

	return FALSE;
}

?>